{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 文生图示例\n",
    "\n",
    "本notebook演示如何使用byzerllm进行文生图操作。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[32m2024-09-15 23:12:45.597\u001b[0m | \u001b[1mINFO    \u001b[0m | \u001b[36mbyzerllm.utils.connect_ray\u001b[0m:\u001b[36mconnect_cluster\u001b[0m:\u001b[36m48\u001b[0m - \u001b[1mJDK 21 will be used (/Users/allwefantasy/.auto-coder/jdk-21.0.2.jdk/Contents/Home)...\u001b[0m\n",
      "2024-09-15 23:12:45,645\tINFO worker.py:1564 -- Connecting to existing Ray cluster at address: 127.0.0.1:6379...\n",
      "2024-09-15 23:12:45,646\tINFO worker.py:1582 -- Calling ray.init() again after it has already been called.\n"
     ]
    },
    {
     "ename": "RayTaskError(JSONDecodeError)",
     "evalue": "\u001b[36mray::UDFWorker.async_apply()\u001b[39m (pid=75491, ip=127.0.0.1, actor_id=9dac348aad3cb1da2a24e2ba32000000, repr=<pyjava.udf.udf_worker.UDFWorker object at 0x195755c90>)\n  File \"/opt/miniconda3/envs/byzerllm/lib/python3.10/json/__init__.py\", line 346, in loads\n    return _default_decoder.decode(s)\n  File \"/opt/miniconda3/envs/byzerllm/lib/python3.10/json/decoder.py\", line 337, in decode\n    obj, end = self.raw_decode(s, idx=_w(s, 0).end())\n  File \"/opt/miniconda3/envs/byzerllm/lib/python3.10/json/decoder.py\", line 355, in raw_decode\n    raise JSONDecodeError(\"Expecting value\", s, err.value) from None\njson.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)\n\nDuring handling of the above exception, another exception occurred:\n\n\u001b[36mray::UDFWorker.async_apply()\u001b[39m (pid=75491, ip=127.0.0.1, actor_id=9dac348aad3cb1da2a24e2ba32000000, repr=<pyjava.udf.udf_worker.UDFWorker object at 0x195755c90>)\n  File \"/opt/miniconda3/envs/byzerllm/lib/python3.10/concurrent/futures/_base.py\", line 451, in result\n    return self.__get_result()\n  File \"/opt/miniconda3/envs/byzerllm/lib/python3.10/concurrent/futures/_base.py\", line 403, in __get_result\n    raise self._exception\n  File \"/opt/miniconda3/envs/byzerllm/lib/python3.10/site-packages/pyjava/udf/udf_worker.py\", line 65, in async_apply\n    resp = await self.apply_func(self.model, v)\n  File \"/Users/allwefantasy/projects/byzer-llm/src/byzerllm/utils/text_generator.py\", line 183, in simple_predict_func\n    v = await llm.async_predict(item)\n  File \"/Users/allwefantasy/projects/byzer-llm/src/byzerllm/utils/text_generator.py\", line 161, in async_predict\n    response = await self.model.async_stream_chat(self.tokenizer,\n  File \"/Users/allwefantasy/projects/byzer-llm/src/byzerllm/saas/siliconflow/__init__.py\", line 148, in async_stream_chat\n    other_params = {\n  File \"/Users/allwefantasy/projects/byzer-llm/src/byzerllm/saas/siliconflow/__init__.py\", line 96, in async_text_to_image\n    lambda: requests.post(url, json=payload, headers=headers)\n  File \"/opt/miniconda3/envs/byzerllm/lib/python3.10/site-packages/requests/models.py\", line 978, in json\n    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)\nrequests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mRayTaskError(JSONDecodeError)\u001b[0m             Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[2], line 33\u001b[0m\n\u001b[1;32m     30\u001b[0m     display(Image(filename\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgenerated_image.png\u001b[39m\u001b[38;5;124m\"\u001b[39m))\n\u001b[1;32m     32\u001b[0m \u001b[38;5;66;03m# 示例使用\u001b[39;00m\n\u001b[0;32m---> 33\u001b[0m \u001b[43mgenerate_and_display_image\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m一个拥有战斗铠甲的熊猫手持 auto-coder.chat 的横幅\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "Cell \u001b[0;32mIn[2], line 12\u001b[0m, in \u001b[0;36mgenerate_and_display_image\u001b[0;34m(prompt, size, quality)\u001b[0m\n\u001b[1;32m     10\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mgenerate_and_display_image\u001b[39m(prompt, size\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m1024x1024\u001b[39m\u001b[38;5;124m\"\u001b[39m, quality\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mstandard\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[1;32m     11\u001b[0m     \u001b[38;5;66;03m# 生成图像\u001b[39;00m\n\u001b[0;32m---> 12\u001b[0m     response \u001b[38;5;241m=\u001b[39m \u001b[43mllm\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mchat_oai\u001b[49m\u001b[43m(\u001b[49m\u001b[43mconversations\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[43m{\u001b[49m\n\u001b[1;32m     13\u001b[0m \u001b[43m        \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mrole\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43muser\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m     14\u001b[0m \u001b[43m        \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mcontent\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mjson\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdumps\u001b[49m\u001b[43m(\u001b[49m\u001b[43m{\u001b[49m\n\u001b[1;32m     15\u001b[0m \u001b[43m            \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43minput\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mprompt\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m     16\u001b[0m \u001b[43m            \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43msize\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43msize\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m     17\u001b[0m \u001b[43m            \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mquality\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mquality\u001b[49m\n\u001b[1;32m     18\u001b[0m \u001b[43m        \u001b[49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mensure_ascii\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m)\u001b[49m\n\u001b[1;32m     19\u001b[0m \u001b[43m    \u001b[49m\u001b[43m}\u001b[49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     21\u001b[0m     \u001b[38;5;66;03m# print(response[0].output)\u001b[39;00m\n\u001b[1;32m     22\u001b[0m     \u001b[38;5;66;03m# 解码图像数据\u001b[39;00m\n\u001b[1;32m     23\u001b[0m     image_data \u001b[38;5;241m=\u001b[39m base64\u001b[38;5;241m.\u001b[39mb64decode(response[\u001b[38;5;241m0\u001b[39m]\u001b[38;5;241m.\u001b[39moutput)\n",
      "File \u001b[0;32m~/projects/byzer-llm/src/byzerllm/utils/client/byzerllm_client.py:1213\u001b[0m, in \u001b[0;36mByzerLLM.chat_oai\u001b[0;34m(self, conversations, tools, tool_choice, execute_tool, impl_func, execute_impl_func, impl_func_params, func_params, response_class, response_after_chat, enable_default_sys_message, model, role_mapping, llm_config, only_return_prompt)\u001b[0m\n\u001b[1;32m   1210\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m new_responses\n\u001b[1;32m   1211\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m responses\n\u001b[0;32m-> 1213\u001b[0m res \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_query\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   1214\u001b[0m clean_func \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmapping_clean_func\u001b[38;5;241m.\u001b[39mget(model, \u001b[38;5;28;01mlambda\u001b[39;00m s: s)\n\u001b[1;32m   1216\u001b[0m responses \u001b[38;5;241m=\u001b[39m [\n\u001b[1;32m   1217\u001b[0m     LLMResponse(\n\u001b[1;32m   1218\u001b[0m         output\u001b[38;5;241m=\u001b[39mclean_func(item[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpredict\u001b[39m\u001b[38;5;124m\"\u001b[39m]),\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m   1222\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m item \u001b[38;5;129;01min\u001b[39;00m res\n\u001b[1;32m   1223\u001b[0m ]\n",
      "File \u001b[0;32m~/projects/byzer-llm/src/byzerllm/utils/client/byzerllm_client.py:1887\u001b[0m, in \u001b[0;36mByzerLLM._query\u001b[0;34m(self, model, input_value)\u001b[0m\n\u001b[1;32m   1885\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m json\u001b[38;5;241m.\u001b[39mloads(res[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mvalue\u001b[39m\u001b[38;5;124m\"\u001b[39m][\u001b[38;5;241m0\u001b[39m])\n\u001b[1;32m   1886\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m inst:\n\u001b[0;32m-> 1887\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m inst\n\u001b[1;32m   1888\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m   1889\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m index \u001b[38;5;241m!=\u001b[39m \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m:\n",
      "File \u001b[0;32m~/projects/byzer-llm/src/byzerllm/utils/client/byzerllm_client.py:1877\u001b[0m, in \u001b[0;36mByzerLLM._query\u001b[0;34m(self, model, input_value)\u001b[0m\n\u001b[1;32m   1874\u001b[0m         worker_id \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpin_model_worker_mapping\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmeta\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m)            \n\u001b[1;32m   1876\u001b[0m [index, worker] \u001b[38;5;241m=\u001b[39m ray\u001b[38;5;241m.\u001b[39mget(udf_master\u001b[38;5;241m.\u001b[39mget\u001b[38;5;241m.\u001b[39mremote(worker_id))\n\u001b[0;32m-> 1877\u001b[0m res \u001b[38;5;241m=\u001b[39m \u001b[43mray\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[43mworker\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43masync_apply\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mremote\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnew_input_value\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   1879\u001b[0m event_result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_trigger_event(\n\u001b[1;32m   1880\u001b[0m     EventName\u001b[38;5;241m.\u001b[39mAFTER_CALL_MODEL, \u001b[38;5;28mself\u001b[39m, model, json\u001b[38;5;241m.\u001b[39mloads(res[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mvalue\u001b[39m\u001b[38;5;124m\"\u001b[39m][\u001b[38;5;241m0\u001b[39m])\n\u001b[1;32m   1881\u001b[0m )\n\u001b[1;32m   1882\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m event_result \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n",
      "File \u001b[0;32m/opt/miniconda3/envs/byzerllm/lib/python3.10/site-packages/ray/_private/auto_init_hook.py:21\u001b[0m, in \u001b[0;36mwrap_auto_init.<locals>.auto_init_wrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m     18\u001b[0m \u001b[38;5;129m@wraps\u001b[39m(fn)\n\u001b[1;32m     19\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mauto_init_wrapper\u001b[39m(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[1;32m     20\u001b[0m     auto_init_ray()\n\u001b[0;32m---> 21\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfn\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m/opt/miniconda3/envs/byzerllm/lib/python3.10/site-packages/ray/_private/client_mode_hook.py:103\u001b[0m, in \u001b[0;36mclient_mode_hook.<locals>.wrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m    101\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m func\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m \u001b[38;5;241m!=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124minit\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01mor\u001b[39;00m is_client_mode_enabled_by_default:\n\u001b[1;32m    102\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mgetattr\u001b[39m(ray, func\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m)(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[0;32m--> 103\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m/opt/miniconda3/envs/byzerllm/lib/python3.10/site-packages/ray/_private/worker.py:2623\u001b[0m, in \u001b[0;36mget\u001b[0;34m(object_refs, timeout)\u001b[0m\n\u001b[1;32m   2617\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m   2618\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInvalid type of object refs, \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mtype\u001b[39m(object_refs)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m, is given. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m   2619\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mobject_refs\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m must either be an ObjectRef or a list of ObjectRefs. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m   2620\u001b[0m     )\n\u001b[1;32m   2622\u001b[0m \u001b[38;5;66;03m# TODO(ujvl): Consider how to allow user to retrieve the ready objects.\u001b[39;00m\n\u001b[0;32m-> 2623\u001b[0m values, debugger_breakpoint \u001b[38;5;241m=\u001b[39m \u001b[43mworker\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_objects\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobject_refs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtimeout\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   2624\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i, value \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28menumerate\u001b[39m(values):\n\u001b[1;32m   2625\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(value, RayError):\n",
      "File \u001b[0;32m/opt/miniconda3/envs/byzerllm/lib/python3.10/site-packages/ray/_private/worker.py:861\u001b[0m, in \u001b[0;36mWorker.get_objects\u001b[0;34m(self, object_refs, timeout)\u001b[0m\n\u001b[1;32m    859\u001b[0m     global_worker\u001b[38;5;241m.\u001b[39mcore_worker\u001b[38;5;241m.\u001b[39mdump_object_store_memory_usage()\n\u001b[1;32m    860\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(value, RayTaskError):\n\u001b[0;32m--> 861\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m value\u001b[38;5;241m.\u001b[39mas_instanceof_cause()\n\u001b[1;32m    862\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m    863\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m value\n",
      "\u001b[0;31mRayTaskError(JSONDecodeError)\u001b[0m: \u001b[36mray::UDFWorker.async_apply()\u001b[39m (pid=75491, ip=127.0.0.1, actor_id=9dac348aad3cb1da2a24e2ba32000000, repr=<pyjava.udf.udf_worker.UDFWorker object at 0x195755c90>)\n  File \"/opt/miniconda3/envs/byzerllm/lib/python3.10/json/__init__.py\", line 346, in loads\n    return _default_decoder.decode(s)\n  File \"/opt/miniconda3/envs/byzerllm/lib/python3.10/json/decoder.py\", line 337, in decode\n    obj, end = self.raw_decode(s, idx=_w(s, 0).end())\n  File \"/opt/miniconda3/envs/byzerllm/lib/python3.10/json/decoder.py\", line 355, in raw_decode\n    raise JSONDecodeError(\"Expecting value\", s, err.value) from None\njson.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)\n\nDuring handling of the above exception, another exception occurred:\n\n\u001b[36mray::UDFWorker.async_apply()\u001b[39m (pid=75491, ip=127.0.0.1, actor_id=9dac348aad3cb1da2a24e2ba32000000, repr=<pyjava.udf.udf_worker.UDFWorker object at 0x195755c90>)\n  File \"/opt/miniconda3/envs/byzerllm/lib/python3.10/concurrent/futures/_base.py\", line 451, in result\n    return self.__get_result()\n  File \"/opt/miniconda3/envs/byzerllm/lib/python3.10/concurrent/futures/_base.py\", line 403, in __get_result\n    raise self._exception\n  File \"/opt/miniconda3/envs/byzerllm/lib/python3.10/site-packages/pyjava/udf/udf_worker.py\", line 65, in async_apply\n    resp = await self.apply_func(self.model, v)\n  File \"/Users/allwefantasy/projects/byzer-llm/src/byzerllm/utils/text_generator.py\", line 183, in simple_predict_func\n    v = await llm.async_predict(item)\n  File \"/Users/allwefantasy/projects/byzer-llm/src/byzerllm/utils/text_generator.py\", line 161, in async_predict\n    response = await self.model.async_stream_chat(self.tokenizer,\n  File \"/Users/allwefantasy/projects/byzer-llm/src/byzerllm/saas/siliconflow/__init__.py\", line 148, in async_stream_chat\n    other_params = {\n  File \"/Users/allwefantasy/projects/byzer-llm/src/byzerllm/saas/siliconflow/__init__.py\", line 96, in async_text_to_image\n    lambda: requests.post(url, json=payload, headers=headers)\n  File \"/opt/miniconda3/envs/byzerllm/lib/python3.10/site-packages/requests/models.py\", line 978, in json\n    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)\nrequests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)"
     ]
    }
   ],
   "source": [
    "import byzerllm\n",
    "import json\n",
    "import base64\n",
    "import matplotlib.pyplot as plt\n",
    "from IPython.display import Image, display\n",
    "\n",
    "# 初始化ByzerLLM\n",
    "llm = byzerllm.ByzerLLM.from_default_model(\"flux_chat\")\n",
    "\n",
    "def generate_and_display_image(prompt, size=\"1024x1024\", quality=\"standard\"):\n",
    "    # 生成图像\n",
    "    response = llm.chat_oai(conversations=[{\n",
    "        \"role\":\"user\",\n",
    "        \"content\": json.dumps({\n",
    "            \"input\": prompt,\n",
    "            \"size\": size,\n",
    "            \"quality\": quality\n",
    "        }, ensure_ascii=False)\n",
    "    }])\n",
    "    \n",
    "    # print(response[0].output)\n",
    "    # 解码图像数据\n",
    "    image_data = base64.b64decode(response[0].output)\n",
    "    \n",
    "    # 保存图像\n",
    "    with open(\"generated_image.png\", \"wb\") as f:\n",
    "        f.write(image_data)\n",
    "    \n",
    "    # 显示图像\n",
    "    display(Image(filename=\"generated_image.png\"))\n",
    "\n",
    "# 示例使用\n",
    "generate_and_display_image(\"一个拥有战斗铠甲的熊猫手持 auto-coder.chat 的横幅\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "你可以通过修改 `generate_and_display_image` 函数的参数来生成不同的图像。例如：\n",
    "\n",
    "```python\n",
    "generate_and_display_image(\"一个未来主义风格的城市景观\", size=\"512x512\", quality=\"hd\")\n",
    "```\n",
    "\n",
    "请注意，你需要确保已经正确设置了OpenAI的API密钥，并且已经部署了 `openai_image_gen` 模型。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
